<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form  :inline="true">
        <el-form-item label="文章类型">
          <el-input v-model="paramKey" placeholder="根据键查询"></el-input>
        </el-form-item>
				<el-form-item>
					<el-button type="warning" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange" highlight-current-row
			style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" >
			</el-table-column>
			<el-table-column type="index"  align="center"  label="序号">
			</el-table-column>
			<el-table-column prop="paramKey" align="center" label="参数键" >
			</el-table-column>
			<el-table-column align="center"  prop="paramValue" label="参数值" >
			</el-table-column>
			<el-table-column   align="center" label="参数类型" >
        <template slot-scope="scope">
          <span v-if="scope.row.type===0" style="color: #5e7382">普通表达项</span>
          <span v-else-if="scope.row.type===1" style="color: green">文件上传表单项</span>

        </template>
			</el-table-column>
			<el-table-column prop="createTime"  align="center" label="添加时间">
			</el-table-column>
      <el-table-column prop="updateTime"  align="center" label="修改时间">
      </el-table-column>
      <el-table-column prop="operatorName"  align="center" label="操作人">
      </el-table-column>
      <el-table-column prop="paramDesc"  align="center" label="参数描述">
      </el-table-column>

			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
				:page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="totals" style="float:right;">
			</el-pagination>
		</el-col>
<!--
  添加或修改页面
  visible.sync: 是否显示模态框
  :close-on-click-model:点空白处是否关模态框
  :rules="saveFormRules" form表单校验规则
  ref="saveForm" form表单的id

 -->
		<!--添加或修改界面-->
		<el-dialog :title="title" :before-close="handleClose" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px"  ref="saveForm" >
        <el-form-item label="参数键">
          <el-input v-model="saveForm.paramKey" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="参数值">
          <el-input v-model="saveForm.paramValue" auto-complete="off" ></el-input>
        </el-form-item>
          <el-form-item label="文章状态" >
            <el-radio-group v-model="saveForm.type">
              <el-radio :label="0">普通表达项</el-radio>
              <el-radio :label="1">文件上传表单项</el-radio>
            </el-radio-group>
          </el-form-item>
        <el-form-item label="操作人">
          <el-input v-model="saveForm.operatorName" auto-complete="off" ></el-input>
        </el-form-item>
        <el-form-item label="参数描述" >
          <el-input v-model="saveForm.paramDesc"  auto-complete="off"></el-input>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="handleClose">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

import {title} from "mockjs/src/mock/random/text";
import th from "element-ui/src/locale/lang/th";

export default {
	data() {
		return {
      paramKey:'',
      // 文件列表 这个是会返回一个数组 包括文件名字 文件路径
      fileList:[],
      options:[],//级联接收的数据
			//1.分页查询相关数据
			totals: 0,//总记录条数
			currentPage: 1,//当前页,要传递到后台的
			pageSize: 5, //每页显示多少条
			tableDatas: [], //当前页数据
			listLoading: false,
      isChange:"",//设置一个属性 专门用来标志我是否有删除附件
      //模态框标题
      title:"",
			//2.高级查询相关数据
			keyword: '',
      changeImgs:[], //定义一个数据，来记录我的附件框是否有值，如果有值，就证明有上传附件，如果没有值，就没有上传附件
			//3.删除相关数据

			//4.批量删除相关数据
			sels: [], //存储多选记录

			//5.添加和修改相关数据
			saveFormVisible: false,
      //查询的form
      searchForm:{
        paramKey:"",
        type:0,

      },
			saveForm: {
        id: null,
        paramKey:"", //参数键
        paramValue:"",//参数值
        type:0,
        operatorName:"",//操作人
        paramDesc:"",//参数描述
			},


		}
	},
	methods: {
    handleChange(file, fileList){
      //我就添加file
      this.changeImgs.push(file);
    },
    handleClose(){
      //关闭对话框之前 先执行此代码
      this.fileList = [];
      this.saveFormVisible = false;
    },
    //上传成功过后的钩子
    handleSuccess(response,file,fileList){
       let {success,resultObj} = response;
       if(success){
         //清空filelist
         //给图片赋值 赋值完毕过后应该立即提交form表单
         this.saveForm.paramPic = resultObj
         //手动提交form表单
         this.saveFormToDB()
       }
    },
   //文件列表移除时候的方法
    handleRemove(file,fileList){
            /*this.changeImgs.slice(0,1);*/
            this.changeImgs.pop();
            //如果点击了移除照片文件 就证明删除了附件
            this.isChange =  true;

    },
    //手动提交form表单
    saveFormToDB(){
      this.$http.post("/param",this.saveForm).then(res=>{
        let {success} = res.data;
        if(success){
          this.saveFormVisible = false;
          //清空记录是否有上传附件的标志
          this.changeImgs = [];
          //清空表单
          this.fileList = [];
          this.getTableData();
        }
      })
    },
		//1.分页查询相关方法
		//1.1.获取列表数据
		getTableData() {
      //开启加载框
      this.listLoading = true;
      //这一坨是负责传到后端的数据 记住前面双引号的名字需要和后端接收到的一样
			let param = {
        "currentPage": this.currentPage,
        "pageSize":this.pageSize,
        "key":this.paramKey,
      }
      this.$http.patch("/param",param).then(res=>{
        let {success,resultObj} = res.data;
        if(success){
           //关闭加载框
          this.listLoading = false;
          this.tableDatas = resultObj.list;
          this.totals = resultObj.total;
        }
      })
		},
		//1.2.选择第几页时触发
		handleCurrentChange(curentPage) {
			this.currentPage = curentPage;
      this.getTableData();
		},
		//1.3.选择每页显示记录条数时触发
		handleSizeChange(pageSize) {
			  this.currentPage = 1;
        this.pageSize = pageSize;
        this.getTableData();
		},

		//2.高级查询相关方法
		keywordQuery() {
      if(this.searchForm.date){
            this.searchForm.startTime =this.searchForm.date[0],
            this.searchForm.endTime = this.searchForm.date[1]
      }else {
            this.searchForm.startTime =null,
            this.searchForm.endTime = null
      }

			this.currentPage = 1;
      this.getTableData();
		},

		//3.删除相关方法
		handleDel(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete("/param/"+row.id).then(res=> {
          let {success} = res.data;
          if (success) {
            this.$message({
              type: 'success',
              message: "删除成功！"
            });
            //删除成功过后需要刷新
            this.getTableData();
          }else {
            this.$message.error(message);
          }
        })
      });
		},


		//4.批量删除相关方法
		//4.1.选项改变时触发
		handleSelectionChange(val) {
      this.sels = val;
		},
		//4.2.批量删除
    handlePatchDel() {
      this.$confirm('您确认删除选中的数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.patch("/param/patchDelete",this.sels).then(res=>{
          let  {success} = res.data;
          if(success){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getTableData();
          }
        })
      });
    },

		//5.添加和修改相关方法
		//5.1.点击添加弹出对话框
		handleAdd() {
      //设置标题
      this.title = "新增参数类型";
      //清空表单
      for(let k in this.saveForm){
        this.saveForm[k] = null;
      }
      //新增应该显示密码框
      this.saveForm["id"] = null;
      this.saveForm["type"] = 0;
      //手动清空图片
      this.fileList = [];
      //弹出窗体
			 this.saveFormVisible = true;
		},
		//5.2.点击修改弹出对话框
		handleEdit(row) {
      //清空isChange的状态
      this.isChange = "";
      //修改标题
      this.title = "编辑文章类型";
      //清空表单
      for(let k in this.saveForm){
        this.saveForm[k] = "";
      }
    //回显数据
      for(let k in row){
        this.saveForm[k] = row[k];
      }
     if(this.saveForm.paramPic){
         this.fileList.push({url:this.saveForm.paramPic})
     }

    //打开模态框
      this.saveFormVisible = true
		},
		//5.3.提交表单数据操作数据库
    saveSubmit() {
      if(!this.saveForm.id){  //新增
        //如果长度大于0就证明有附件上传
        if(this.changeImgs.length){
          this.$refs.upload.submit();
        }else{  //没有附件上传
          this.saveFormToDB();
        }
      }else{  //修改
        //有附件
        if(this.saveForm.paramPic){
          //附件有修改
          if(this.isChange){
            let paramPic = this.saveForm.paramPic;
            //拿到图片url地址最后一根/
            let index = paramPic.lastIndexOf("/");
            //截取字符串，得到附件名
            let objectName = paramPic.substring(index + 1);
            this.$http.delete("/minio/"+objectName).then(res=>{
              let {success} = res.data;
              if(success){ //如果成功，并且有上传新附件
                //有上传新附件
                if(this.changeImgs.length){
                  this.$refs.upload.submit();
                }else{
                  //把图片清空
                  this.saveForm.paramPic = "";
                  //直接走form表单
                  this.saveFormToDB();
                }
              }
            })
          }
          else{
            //直接提交form表单
            this.saveFormToDB();
          }
        }else{  //没有附件
          if(this.changeImgs.length){
            this.$refs.upload.submit();
          }else{
            //把图片清空
            this.saveForm.paramPic = "";
            //直接走form表单
            this.saveFormToDB();
          }
        }
      }
    },
    //获取文章类型树形结构
    getArticleTypeTree(){
      this.$http.patch("/paramType/getTree").then(res=>{
        let{success,resultObj} = res.data;
        if(success){
          this.options = resultObj;
        }
      })
    }
	},
	mounted() {
		this.getTableData();
    //获取文章类型树形结构
    this.getArticleTypeTree();
	}
}

</script>

<style scoped></style>
